#!/usr/bin/env jq
# compile-check-1-compiled_input_output_well_defined -- Checks if all inputs and outputs of compiled processes are well-defined
##
.deepdive_ as $deepdive |

# a handy map for checking whether a relation has a defining process
( $deepdive.execution.processes
| with_entries({ key: (.value.output_ // [])[], value: .key })
) as $process_defining_the_relation |

# check every compiled process
$deepdive.execution.processes | to_entries[] |
.key as $extractorName | .value |

# if inputs are all output by another process
(
    select(.input_ | length > 0) |
    [ .input_[]
    | select(in($process_defining_the_relation) | not)
    ] |
    if length == 0 then "\($extractorName) inputs are all output by other processes"
    else error("\($extractorName) depends on inputs not output by any process: \(map("'\(.)'") | join(" "))")
    end
),

# if output are defined in the schema
(
    (.output_ // [])[] | tostring |
    if startswith("data/") and (ltrimstr("data/") | in($deepdive.schema.relations)
                                                    # XXX assume user is doing the right thing if schema.json is empty
                                                    or ($deepdive.schema.relations | length == 0))
    then
        "\($extractorName) output_relation is defined in schema.json: '\(ltrimstr("data/"))'"
    elif startswith("data/") and (startswith("data/model/") | not) then
        error("\($extractorName) has output_relation undefined in schema.json: '\(.)'")
    else
        "\($extractorName) output is not for database: '\(.)'"
    end
),

empty
